package com.woniuxy.happylife.suggest.config;

import com.rabbitmq.client.Channel;
import com.woniuxy.happylife.entity.Suggests;
import com.woniuxy.happylife.suggest.dao.SuggestionDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;

/**
 * rabbitmq的消费端
 * @Author Fu
 * @Date 2022-02-16 15:25
 */
@Slf4j
@Component
public class MyConsumer {
    @Autowired(required = false)
    SuggestionDao suggestionDao;
    @RabbitListener(queues = "addSuggestQueue")
    public void receiveAddSuggest(Suggests suggests, Message message, Channel channel) throws IOException {
        log.info("接收到了suggests"+suggests.getContent());

        try {
            suggestionDao.insert(suggests);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
            e.printStackTrace();
        }

    }
    @RabbitListener(queues = "queueDead")
    public void receiveDead(Suggests suggests, Message message, Channel channel) throws IOException, InterruptedException {
        System.out.println("接收的消息Dead");
        System.out.println("Dead = " + suggests);
        System.out.println("---------");
        channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
    }
}
